package com.ruibo.tax.mq.send;

import cn.hutool.core.io.FileUtil;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ruibo.tax.constants.SendDataPackageConstants;
import com.ruibo.tax.model.base.PackageHead;
import com.ruibo.tax.model.base.XMLEncodeModel;
import com.ruibo.tax.model.dz.SSDZXX;
import com.ruibo.tax.model.dz.SSDZXXXMLEncodeModel;
import com.ruibo.tax.utils.DateUtils;
import com.ruibo.tax.utils.PathZip;
import com.ruibo.tax.utils.XMLUtils;
import com.ruibo.tax.utils.ZipUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.ArrayList;


/**
 * 数据发送功能
 * 1、环保对税务的业务数据 压缩包数据
 * 2、环保对税务的对账数据 压缩包数据
 */
@Slf4j
@Component
public class ReconciliationSendZIPMessage {

    //    环保对税务的队列
    @Resource(name = "QLHB2SW")
    MQQueue hb2swMQQueue;

    //    环保对税务的对账队列
    @Resource(name = "DZ_QLHB2SW")
    MQQueue dz_hb2swMQQueue;


    @Autowired
    private PathZip pathZip;

    @Value("${baseFilePath}")
    private String baseFilePath;

    @Value("${beiFenFilePath}")
    private String beiFenFilePath;

    @Autowired
    private XMLUtils xmlUtils;


    private static final Integer BUFFER_LEN = 2048;

    /**
     * 环保部门向税务部门发送压缩数据的方法
     *
     * @param
     */
    public void sendZipMsgToQLHB2SW(String fileName) {
        sendZipMessage(fileName, hb2swMQQueue);
    }

    /**
     * 发送对账环保对税务
     */
    public void sendMsgToDZ_QLHB2SW(String fileName) {
        sendZipMessage(fileName, dz_hb2swMQQueue);
    }

    /**
     * 发送实时对账信息
     *
     * @param heads
     * @throws IllegalAccessException
     */
    public void sendRealTimeDZMsg(PackageHead heads) throws IllegalAccessException {
        //在这里需要发送实时对账信息

        SSDZXX ssdzxx = new SSDZXX();
        ssdzxx.setSJBBH(heads.getSJBBH());
        ssdzxx.setSJBLX(heads.getSJBLX());
        SSDZXXXMLEncodeModel model = new SSDZXXXMLEncodeModel();
        SSDZXXXMLEncodeModel ssdzxxxmlEncodeModel = xmlUtils.data(model, ssdzxx, SendDataPackageConstants.SSDZXX);
        String msg = ssdzxxxmlEncodeModel.toSendData(Charset.forName("GBK"));
        MQMessage outMsg = new MQMessage();
        MQPutMessageOptions pmo = new MQPutMessageOptions();
        try {
            byte[] bytes = ZipUtils.zip(msg.getBytes());
            outMsg.write(bytes);
            dz_hb2swMQQueue.put(outMsg, pmo);
            outMsg.clearMessage();
            log.info("发送实时对账消息完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 执行zip数据发送
     *
     * @param fileName
     * @param mqQueue
     */
    private void sendZipMessage(String fileName, MQQueue mqQueue) {
        MQMessage outMsg = new MQMessage();
        MQPutMessageOptions pmo = new MQPutMessageOptions();
        FileInputStream fis = null;
        try {
            log.info("开始发送消息");
            fis = new FileInputStream(new File(fileName));
            byte buffer[] = new byte[BUFFER_LEN];
            int count = 0;
            while (true) {
                count = fis.read(buffer, 0, BUFFER_LEN);
                if (count == -1) {
                    break;
                }
                outMsg.write(buffer);
                if (count < BUFFER_LEN) {
                    log.info("数据读取完毕");
                }
            }
            mqQueue.put(outMsg, pmo);
            outMsg.clearMessage();
            log.info("发送消息完成");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 对账文件处理并发送对账数据
     */
    public synchronized void handlerHZDZXXZipFile() {

        log.info("对账数据处理");
        //  执行完成之后进行文件压缩
        pathZip.zipFiles(baseFilePath + SendDataPackageConstants.HZDZXX, baseFilePath + SendDataPackageConstants.HZDZXX + ".zip");
        //执行数据包发送
        sendMsgToDZ_QLHB2SW(baseFilePath + SendDataPackageConstants.HZDZXX + ".zip");
        // 执行备份   精确到毫秒
        String dateTimeFormat = DateUtils.localDateTimeFormat(LocalDateTime.now(), "yyyyMMddhhmmssSSS");
        try {
            File desFile = new File(beiFenFilePath);
            if (!desFile.exists()) {
                desFile.mkdirs();
            }
            pathZip.zipFiles(baseFilePath + SendDataPackageConstants.HZDZXX, beiFenFilePath + SendDataPackageConstants.HZDZXX + dateTimeFormat + ".zip");
        } catch (Exception e) {
            log.error("备份压缩文件失败！");
            return;
        }
        // 删除源文件目录
        FileUtil.del(new File(baseFilePath + SendDataPackageConstants.HZDZXX));
        // 删除源文件目录
        FileUtil.del(new File(baseFilePath + SendDataPackageConstants.HZDZXX + ".zip"));
    }
}
